/*=========================================================
  LHW开发   这个是1602精简版本 删掉图形化界面
  邮箱:1281702594@qq.com
  采用CC协议 https://creativecommons.org/licenses/by-nc-nd/2.5/cn/
  提示：有个小彩蛋
  =========================================================*/
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
/*=========================================================
                         变量
  =========================================================*/
#define RxPin A0 //RX
#define TxPin 13 //TX
byte ADCMod = 0; //4-0 8-1 16-2 32-3 128-4 设置采样深度
byte TFL = 21; //过滤掉的电平 用于过滤干扰  ROOM-2 中 表示发送状态
byte Buffer[128]; //记录波形
int ML, MS; //最长最短间隔
bool EL = false; //ROOM-1:电平情况 ROOM-2:是否编辑文本
bool NEL = false; //现在电平情况  ROOM-2 为TX状态
long CIT1, CIT2; //开始和上一次时间
int CILTF = 10; //这个值由ML与LS决定  ROOM-2为发送进度
int DTO = 90; //接收超时    在TX模式中为最低间隔延迟
byte DTT[5]; //缓存的数据表 0代表在点的间隔低电平 1代表点 2代表划
byte DTL = 255; //缓存写入的位置 255代为禁用 也就是说没信号   在ROOM-2代表是否连续发信
//码库
const int MH[36] PROGMEM = {
  12222,
  11222,
  11122,
  11112,
  11111,
  21111,
  22111,
  22211,
  22221,
  22222,
  12000,
  21110,
  21210,
  21100,
  10000,
  11210,
  22100,
  11110,
  11000,
  12220,
  21200,
  12110,
  22000,
  21000,
  22200,
  12210,
  22120,
  12100,
  11100,
  20000,
  11200,
  11120,
  12200,
  21120,
  21220,
  22110
};
const byte ME[36] PROGMEM = {
  49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
};  //字码库对应ascii
byte TmpString[32]; //接收数据缓存显示区
byte EGG[10] = {74, 73, 78, 71, 74, 85, 67, 65, 84, 83};   //哈哈，我才不会告诉你这是什么，或许可以发送来看看？
const unsigned char EggBmp[] PROGMEM = {0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0xf8, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2, 0x00, 0x01, 0x20, 0x00, 0x00, 0x05, 0xf8, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x00, 0x01, 0x10, 0x00, 0x00, 0x0b, 0xf8, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xc0, 0x01, 0x0c, 0x00, 0x00, 0x13, 0xf8, 0x01, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x20, 0x01, 0x03, 0x00, 0x00, 0x27, 0xf8, 0x00, 0xf0, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xfe, 0x18, 0x01, 0x00, 0xe1, 0x80, 0x47, 0xf8, 0x00, 0x77, 0x80, 0x00, 0x18, 0x00, 0x00, 0x00, 0xff, 0x04, 0x01, 0x00, 0x10, 0xc0, 0x8f, 0xf8, 0x00, 0x7f, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0xff, 0x82, 0x00, 0x80, 0x06, 0x21, 0x0f, 0xf8, 0x01, 0xff, 0xc0, 0x00, 0x3c, 0x04, 0x00, 0x00, 0xff, 0xc1, 0x9f, 0xc0, 0x02, 0x26, 0x1f, 0xf8, 0x0f, 0xfc, 0x00, 0x70, 0x3c, 0x06, 0x00, 0x00, 0xff, 0xc0, 0x60, 0x30, 0x00, 0x28, 0x1f, 0xf8, 0x3f, 0xc0, 0x01, 0xf0, 0x3c, 0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 0x31, 0x8d, 0xb8, 0x7c, 0x3c, 0xc3, 0x60, 0x7f, 0xff, 0xff, 0x80, 0x0f, 0xff, 0xbf, 0xe0, 0x02, 0x31, 0xff, 0xb8, 0xf8, 0x6c, 0x45, 0x00, 0x7f, 0xef, 0xff, 0xc0, 0x1f, 0xff, 0xff, 0xe0, 0x0e, 0x31, 0xf9, 0xf0, 0xf8, 0x0c, 0x7e, 0xc0, 0x7f, 0xc7, 0xff, 0xc0, 0x1f, 0xff, 0xbf, 0xe0, 0x1e, 0x31, 0xf8, 0xe0, 0xf8, 0x1c, 0x7d, 0x80, 0x7f, 0xc3, 0xff, 0x80, 0x1f, 0xfe, 0x1f, 0xe0, 0x3e, 0x30, 0x00, 0x00, 0xf8, 0x00, 0x3b, 0x00, 0x7f, 0x80, 0x07, 0x00, 0x0f, 0x00, 0x1f, 0xc0, 0x7c, 0x70, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x3f, 0x80, 0xe0, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x0c, 0x30, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x3f, 0x03, 0x7c, 0x00, 0x01, 0xfe, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x1f, 0x06, 0xde, 0x00, 0x06, 0xfb, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x0c, 0xcf, 0x00, 0x0e, 0x79, 0x83, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x18, 0xff, 0x80, 0x07, 0xfc, 0xc3, 0x03, 0xba, 0x4e, 0xe9, 0x31, 0x9d, 0xc3, 0x19, 0xb0, 0x04, 0x19, 0xfd, 0x00, 0x17, 0xec, 0xc1, 0x01, 0x13, 0x50, 0x49, 0x4a, 0x4a, 0x04, 0xa5, 0x50, 0x08, 0x18, 0xbf, 0x00, 0x07, 0xfc, 0xc0, 0x81, 0x13, 0x56, 0x49, 0x43, 0xc9, 0x84, 0x25, 0x50, 0x08, 0x0c, 0x9b, 0x47, 0x16, 0xe9, 0x80, 0x81, 0x12, 0xd2, 0x49, 0x4a, 0x48, 0x44, 0xa5, 0x50, 0x08, 0x06, 0x82, 0x05, 0x0a, 0x09, 0x81, 0x03, 0x3a, 0x4e, 0xc6, 0x32, 0x4b, 0x93, 0x19, 0x50, 0x04, 0x03, 0x3d, 0x07, 0x05, 0xb2, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x02, 0x01, 0xc0, 0x02, 0x00, 0x00, 0x06, 0x01, 0xe4, 0xff, 0xe0, 0x00, 0x00, 0x02, 0x00, 0x00, 0x77, 0xf8, 0x00, 0x04, 0x00, 0x00, 0x7f, 0xe7, 0x04, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x08, 0x00, 0x00, 0x40, 0x41, 0x04, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7f, 0xf0, 0x00, 0x08, 0x00, 0x00, 0x08, 0x0f, 0xfc, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x7f, 0xf0, 0x00, 0x10, 0x00, 0x00, 0xff, 0xc1, 0x0c, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x7f, 0xf0, 0x00, 0x20, 0x00, 0x00, 0x31, 0x03, 0x8c, 0x09, 0x80, 0x00, 0x00, 0x00, 0x20, 0x00, 0x3f, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x32, 0x03, 0x54, 0x08, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x18, 0x10, 0x00, 0x80, 0x00, 0x00, 0x1e, 0x05, 0x54, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x10, 0x08, 0x01, 0x00, 0x00, 0x00, 0x0e, 0x01, 0x44, 0x08, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x39, 0x81, 0x5c, 0x08, 0x00, 0x00, 0x00, 0x00, 0x02, 0x81, 0x16, 0x02, 0x1c, 0x00, 0x00, 0x00, 0x60, 0x81, 0x0c, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf2, 0x10, 0x02, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x40, 0x20, 0x01, 0x84, 0x00, 0x7f, 0xc0, 0x20, 0x08, 0x02, 0x10, 0xff, 0xe2, 0x24, 0x00, 0x1f, 0xfc, 0x20, 0x00, 0xff, 0x00, 0x0c, 0x00, 0x20, 0x18, 0x0f, 0xfe, 0x04, 0x07, 0xfe, 0x00, 0x3f, 0xf0, 0x20, 0x00, 0xff, 0xe0, 0x0c, 0x07, 0xff, 0x7f, 0xc7, 0xf8, 0x04, 0x06, 0x24, 0x00, 0xff, 0xf0, 0x1e, 0x40, 0xff, 0xf8, 0x0c, 0x00, 0x20, 0x40, 0xcf, 0x18, 0x0e, 0x07, 0xfe, 0x01, 0xff, 0xf0, 0x10, 0x20, 0x3f, 0xf8, 0xff, 0xe0, 0x60, 0x40, 0xc7, 0xfe, 0x0f, 0x8e, 0x00, 0x01, 0xff, 0xf4, 0x10, 0x00, 0x3f, 0xfc, 0x0e, 0x00, 0xe0, 0x7f, 0xc7, 0xfe, 0x36, 0xcf, 0xfe, 0x03, 0xff, 0xe4, 0x10, 0x00, 0x3f, 0xfc, 0x0e, 0x01, 0xa0, 0x40, 0xc7, 0x90, 0x26, 0x42, 0x64, 0x03, 0xff, 0xe0, 0x0d, 0x80, 0x9f, 0xfc, 0x1b, 0x03, 0x20, 0x40, 0xcf, 0xfe, 0xc6, 0x46, 0x7e, 0x03, 0xff, 0xf2, 0x07, 0x80, 0x9f, 0xfc, 0x31, 0x8e, 0x20, 0x40, 0xc3, 0xc4, 0x06, 0x03, 0xe4, 0x01, 0xff, 0xf2, 0x0c, 0xc1, 0x3f, 0xfc, 0x60, 0xe0, 0x60, 0x7f, 0xc7, 0xfe, 0x06, 0x07, 0x7e, 0x01, 0xff, 0xf1, 0xff, 0xfe, 0x3f, 0xfc, 0xc0, 0x40, 0x60, 0x40, 0xc2, 0x64, 0x06, 0x02, 0x64, 0x00, 0xff, 0xf0, 0x7f, 0xf4, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0xfb, 0xf8, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte EGGN;//彩蛋计数
/*====================================================================
                             软重启函数
  ====================================================================*/
void(* resetFunc) (void) = 0; //制造重启命令
/*=========================================================
                     只循环一次
  =========================================================*/
void setup()
{
  Serial.begin(115200);   //初始化串口比特率
  lcd.begin(16, 2);
  pinMode(RxPin, INPUT); //初始化RXpin口
  ADCSET();
}
/*=========================================================
                     不停循环
  =========================================================*/
void loop()
{
  sampling(); //采样
  Minterval(); //计算间隔

}
/*=========================================================
                     清除数据缓冲
  =========================================================*/
void CM()
{
  for (int i = 0; i < 5; i++) DTT[i] = 0;
}
/*=========================================================
                     显示
  =========================================================*/
void draw() {
  for (byte i = 0; i < 32; i++) {
    if (i == 0) lcd.setCursor(0, 0);
    if (i == 16) lcd.setCursor(0, 1);
    lcd.write(TmpString[i]);
  }
}
/*=========================================================
                     译码
  =========================================================*/
void translation()
{
  if (CIT2 >= ML - CILTF && CIT2 <= ML + CILTF && ML != 0 && EL == true) {
    //合法数据 写入缓存
    DTT[DTL] = 2;
    DTL++;
  } else {
    if (CIT2 >= MS - CILTF && CIT2 <= MS + CILTF && MS != 0 && EL == true) {
      //合法数据 写入缓存
      DTT[DTL] = 1;
      DTL++;
    } /*else {
      if (CIT2 >= MS - CILTF && CIT2 <= MS + CILTF && MS != 0 && EL == false) {
        //合法数据 写入缓存
        DTT[DTL] = 0;
        DTL++;
      }
    }*/
  }
  TOStr(); //超时合并字符串
}
/*=========================================================
                     超时合并字符串
  =========================================================*/
void TOStr()
{
  if (( millis() / 10.0) >= CIT1 + DTO || DTL > 4 ) { //译码时间超时 确认不在有数据 数据合并
    //数据接收完成 查询数据库
    String TXT;
    for (int i = 0; i < 5; i++) {   //把接收区缓存合成字符串
      TXT = TXT + DTT[i];
    }
    Serial.println(TXT);
    for (int i = 0; i < 36; i++) {    //匹配数据库信号
      if (String(pgm_read_word_near(&MH[i])) == TXT) {
        Serial.println(char(pgm_read_byte_near(&ME[i])));
        //执行显示区数据移位
        for (byte i = 0; i < 31; i++) {
          TmpString[i] = TmpString[i + 1];
        }
        TmpString[31] = pgm_read_byte_near(&ME[i]); //写到显示区最后的位置
        CM();
      }
    }
    DTL = 255; //无信号 禁用译码
    draw(); //显示画面
  }
}
/*=========================================================
                     计算间隔
  =========================================================*/
void Minterval()
{
  if (map(Buffer[127], 63, 31, 0, 31) >= TFL) {
    NEL = true;
  } else {
    NEL = false; //根据过滤获取电平情况
  }
  if (NEL != EL) {
    //电平发生了变化
    CIT2 = ( millis() / 10.0) - CIT1;
    CIT1 = millis() / 10.0; //获取开始计算时的时间
    if (DTL != 255) translation(); //译码
    //计算最长和最短电平
    if (EL == true) {
      if (CIT2 > MS ) {
        if (CIT2 >= ML - CILTF && CIT2 <= ML + CILTF || ML == 0) {
          ML = CIT2;
          DTO = ML + CILTF;
        } else {
          if (CIT2 > ML && CIT2 <= ML * 4) {
            ML = CIT2;
            DTO = ML + CILTF;
          }
        }
      }
    } else {
      //低电平
      if (CIT2 >= DTO) {
        //在稍微长的时间收到信号  可能是数据开头 到这里代表已经成功学习信号规律
        DTL = 0; //写入位置为0
        CM();
      }
    }
    if (CIT2 <= ML - CILTF && CIT2 > 5) {
      MS = CIT2;
    }
    if (ML != 0) {
      CILTF = ((ML + MS) / 2.0) * 0.6;
    }
    EL = !EL; //反转电平状态
  }

  if (( millis() / 10.0) >= CIT1 + DTO && DTL != 255) {
    TOStr(); //超时
  }
}
/*=========================================================
                     采样和移位
  =========================================================*/
void sampling()
{
  //执行数据移位
  for (byte i = 0; i < 127; i++) {
    Buffer[i] = Buffer[i + 1];
  }
  //执行采样

  Buffer[127] = map(analogRead(RxPin), 0, 1023, 63, 31);
}
/*=========================================================
                     ADC深度设置
  =========================================================*/
void ADCSET()
{
  if (ADCMod == 0) setP4();
  else if (ADCMod == 1) setP8();
  else if (ADCMod == 2) setP16();
  else if (ADCMod == 3) setP32();
  else if (ADCMod == 4) setP128();
}
/*=========================================================
                     ADC深度设置
  =========================================================*/
void setP32( )
{
  ADCSRA |=  (1 << ADPS2);  // 1
  ADCSRA &=  ~(1 << ADPS1);  // 0
  ADCSRA |=  (1 << ADPS0);  // 1
}
void setP16( )
{
  ADCSRA |=  (1 << ADPS2);  // 1
  ADCSRA &=  ~(1 << ADPS1);  // 0
  ADCSRA &=  ~(1 << ADPS0);  // 0
}
void setP8( )    // prescaler = 8
{
  ADCSRA &=  ~(1 << ADPS2);  // 0
  ADCSRA |=  (1 << ADPS1);  // 1
  ADCSRA |=  (1 << ADPS0);  // 1
}
void setP4( )    // prescaler = 4
{
  ADCSRA &=  ~(1 << ADPS2);  // 0
  ADCSRA |=  (1 << ADPS1);  // 1
  ADCSRA &=  ~(1 << ADPS0);  // 0
}
void setP128( )   // 默認就是這樣
{
  ADCSRA |=  (1 << ADPS2);  // 1
  ADCSRA |=  (1 << ADPS1);  // 1
  ADCSRA |=  (1 << ADPS0);  // 1
} // setP128

